//==========================================================
// 此代码由TilverNET代码生成工具自动生成，请不要修改
// 代码生成器版本号：1.2
// 作者：landy
// 联系方式：(QQ)86566178
// 创建时间：2014/2/18 16:52:42
//==========================================================

using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using Tilver.Data;
using Tilver.Data.DataPager;

namespace Tilver.Oracle.DAL.Scott
{
	#region EO

	/// <summary>
	///  《SCOTT.DEPT(实体类)》
	/// </summary>
	[Serializable]
	public class DEPTEO : IRowMapper<DEPTEO>
	{
		protected bool _hasOriginal = false;
		protected decimal _dEPTNO;
		protected decimal _originalDEPTNO;
		protected string _dNAME;
		protected string _lOC;

		/// <summary>
		/// 主键是否有原始值（即数据库中保存的值），用于实体对象更新时
		/// </summary>
		public bool HasOriginal
		{
			get { return _hasOriginal; }
		}


		/// <summary>
		///  《[主键]  [NUMBER]》
		/// </summary>
		public decimal DEPTNO
		{
			get { return _dEPTNO; }
			set { _dEPTNO = value;}
		}

		/// <summary>
		///  《数据库中的原始主键值副本》
		/// </summary>
		public decimal OriginalDEPTNO
		{
			get { return _originalDEPTNO; }
			set { 
				_hasOriginal = true;
				_originalDEPTNO = value;
			}
		}

		/// <summary>
		///  《[字段]  [VARCHAR2]》
		/// </summary>
		public string DNAME
		{
			get { return _dNAME; }
			set { _dNAME = value;}
		}

		/// <summary>
		///  《[字段]  [VARCHAR2]》
		/// </summary>
		public string LOC
		{
			get { return _lOC; }
			set { _lOC = value;}
		}

		/// <summary>
		/// 默认构造函数
		/// </summary>
		public DEPTEO() 
		{
		}

		/// <summary>
		/// 自定义构造函数
		/// </summary>
		/// <param name = "dEPTNO"></param>
		/// <param name = "dNAME"></param>
		/// <param name = "lOC"></param>
		public DEPTEO(decimal dEPTNO, string dNAME, string lOC) 
		{
			_dEPTNO = dEPTNO;
			_dNAME = dNAME;
			_lOC = lOC;
		}


		/// <summary>
		/// 将结果集映射成实体对象
		/// </summary>
		/// <param name = "reader">只进结果集流</param>
		/// <return></return>
		public DEPTEO MapRow(IDataReader reader)
		{
			var ret = new DEPTEO();
			ret.DEPTNO = (decimal)reader["DEPTNO"];
			ret.OriginalDEPTNO = ret.DEPTNO;
			ret.DNAME = (reader["DNAME"] is DBNull) ? null : (string)reader["DNAME"];
			ret.LOC = (reader["LOC"] is DBNull) ? null : (string)reader["LOC"];
			return ret;
		}

		/// <summary>
		/// 将结果集映射成实体对象，返回映射方法
		/// </summary>
		/// <return></return>
		public Func<IDataReader, DEPTEO> GetMapper()
		{
			return MapRow;
		}


	}


	#endregion
	#region MO

	/// <summary>
	///  《SCOTT.DEPT(逻辑类)》
	/// </summary>
	public class DEPTMO : DbManagerBase
	{

		#region 构造函数
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name = "routeData">路由数据</param>
		public DEPTMO(object routeData = null) : base(routeData) { }

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name = "connectionStringName">连接字符串名称</param>
		public DEPTMO(string connectionStringName) : base(connectionStringName) { }

		#endregion

		#region 私有方法
		/// <summary>
		/// 数据库访问操作方法
		/// </summary>
		/// <param name = "sql">SQL语句</param>
		/// <return>返回数据操作对象</return>
		private DbAdoBase AdoBase(string sql)
		{
			return DataBase.CreateDbAdo(sql, CommandType.Text);
		}

		#endregion

		#region Select方法集合
		/// <summary>
		/// 按主键查询
		/// </summary>
		/// <param name = "dEPTNO"></param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public DEPTEO SelectByPK(decimal dEPTNO, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			sql.AppendFormat(@"SELECT ""DEPTNO"", ""DNAME"", ""LOC"" FROM ""SCOTT"".""DEPT"" WHERE ""DEPTNO""=:DEPTNO ");
			return AdoBase(sql.ToString())
							.AddInParameter(":DEPTNO", dEPTNO, DbType.VarNumeric, 0)
							.ExecuteReader(tm).MapToSingle(new DEPTEO().GetMapper());
		}


		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "dEPTNO"></param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<DEPTEO> SelectByDEPTNO(decimal dEPTNO, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} ""DEPTNO"", ""DNAME"", ""LOC"" FROM ""SCOTT"".""DEPT"" WHERE ""DEPTNO""=:DEPTNO");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), string.Empty);
			sql.Append((top > 0) ? ((sql.ToString().IndexOf("WHERE", StringComparison.OrdinalIgnoreCase) > 0) ? string.Format(" AND ROWNUM<={0} ", top) : string.Format(" WHERE ROWNUM<={0} ", top)) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter(":DEPTNO", dEPTNO, DbType.VarNumeric, 0)
							.ExecuteList(new DEPTEO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "dNAME"></param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<DEPTEO> SelectByDNAME(string dNAME, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} ""DEPTNO"", ""DNAME"", ""LOC"" FROM ""SCOTT"".""DEPT"" WHERE ""DNAME""=:DNAME");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), string.Empty);
			sql.Append((top > 0) ? ((sql.ToString().IndexOf("WHERE", StringComparison.OrdinalIgnoreCase) > 0) ? string.Format(" AND ROWNUM<={0} ", top) : string.Format(" WHERE ROWNUM<={0} ", top)) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter(":DNAME", dNAME, DbType.AnsiString, 14)
							.ExecuteList(new DEPTEO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "lOC"></param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<DEPTEO> SelectByLOC(string lOC, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} ""DEPTNO"", ""DNAME"", ""LOC"" FROM ""SCOTT"".""DEPT"" WHERE ""LOC""=:LOC");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), string.Empty);
			sql.Append((top > 0) ? ((sql.ToString().IndexOf("WHERE", StringComparison.OrdinalIgnoreCase) > 0) ? string.Format(" AND ROWNUM<={0} ", top) : string.Format(" WHERE ROWNUM<={0} ", top)) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter(":LOC", lOC, DbType.AnsiString, 13)
							.ExecuteList(new DEPTEO().GetMapper(), tm);
		}

		/// <summary>
		/// 按自定义条件查询
		/// </summary>
		/// <param name = "where">自定义条件,where子句</param>
		/// <param name = "top">获取行数</param>
		/// <param name = "sort">排序表达式</param>
		/// <param name = "paras">where子句中定义的参数集合</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<DEPTEO> Select(string where, int top, string sort, IEnumerable<DbParameter> paras, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} ""DEPTNO"", ""DNAME"", ""LOC"" FROM ""SCOTT"".""DEPT"" ");
			if (!string.IsNullOrEmpty(where)) format.AppendFormat(" WHERE {0} ", where);
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), string.Empty);
			sql.Append((top > 0) ? ((sql.ToString().IndexOf("WHERE", StringComparison.OrdinalIgnoreCase) > 0) ? string.Format(" AND ROWNUM<={0} ", top) : string.Format(" WHERE ROWNUM<={0} ", top)) : string.Empty);
			return AdoBase(sql.ToString())
							.AddParameters(paras)
							.ExecuteList(new DEPTEO().GetMapper(), tm);
		}

		/// <summary>
		/// 分页查询
		/// </summary>
		/// <param name = "where">条件表达式</param>
		/// <param name = "sort">排序表达式</param>
		/// <param name = "paras">传入参数</param>
		/// <param name = "pageSize">页大小</param>
		/// <param name = "pageIndex">页码</param>
		/// <param name = "recordCount">总记录数，由于计算总记录数影响数据库性能，只有小于或等于0的情况下才返回总记录数</param>
		/// <return>分页数据</return>
		public List<DEPTEO> SelectPager(string where, string sort, IList<DbParameter> paras, long pageSize, long pageIndex, ref long recordCount)
		{
			var sql = new StringBuilder();
			sql.AppendFormat(@"SELECT ""DEPTNO"", ""DNAME"", ""LOC"" FROM ""SCOTT"".""DEPT""");
			if (!string.IsNullOrEmpty(where)) sql.AppendFormat(" WHERE {0} ", where);
			if (!string.IsNullOrEmpty(sort)) sql.AppendFormat(" ORDER BY {0} ", sort);
			var pager = this.DataBase.CreatePager(sql.ToString(), pageSize, "DEPTNO");
			pager.AddParameters(paras);
			if (recordCount <= 0)
			{
				recordCount = pager.GetRecordCount();
			}
			return pager.GetPageData(pageIndex).MapToList(new DEPTEO().GetMapper());
		}

		#endregion

		#region Insert方法集合
		/// <summary>
		/// 插入数据，有自增列时，item中自增字段为新增值，否则，返回影响的行数
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "item">要插入的实体对象</param>
		/// <return></return>
		public int Insert(DEPTEO item, TransactionManager tm = null)
		{
			const string sql = @"INSERT INTO ""SCOTT"".""DEPT"" (""DEPTNO"", ""DNAME"", ""LOC"") VALUES (:DEPTNO, :DNAME, :LOC) ";
			return AdoBase(sql)
												.AddInParameter(":DEPTNO", item.DEPTNO, DbType.VarNumeric, 0)
												.AddInParameter(":DNAME", item.DNAME != null ?  (object)item.DNAME : DBNull.Value, DbType.AnsiString, 14)
												.AddInParameter(":LOC", item.LOC != null ?  (object)item.LOC : DBNull.Value, DbType.AnsiString, 13)
												.ExecuteNonQuery(tm);
		}
		#endregion

		#region Update方法集合
		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "item">要更新的实体对象</param>
		/// <param name = "tm">事务管理对象</param>
		/// <return></return>
		public int Update(DEPTEO item, TransactionManager tm = null)
		{
			const string sql = @"UPDATE ""SCOTT"".""DEPT"" SET ""DEPTNO""=:DEPTNO, ""DNAME""=:DNAME, ""LOC""=:LOC WHERE ""DEPTNO""=:OriginalDEPTNO";
			return AdoBase(sql)
							.AddInParameter(":OriginalDEPTNO", (item.HasOriginal) ? item.OriginalDEPTNO : item.DEPTNO, DbType.VarNumeric, 0)
							.AddInParameter(":DEPTNO", item.DEPTNO, DbType.VarNumeric, 0)
							.AddInParameter(":DNAME", item.DNAME != null ?  (object)item.DNAME : DBNull.Value, DbType.AnsiString, 14)
							.AddInParameter(":LOC", item.LOC != null ?  (object)item.LOC : DBNull.Value, DbType.AnsiString, 13)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "dEPTNO"></param>
		/// <param name = "dNAME"></param>
		/// <return></return>
		public virtual int UpdateDNAME(decimal dEPTNO, string dNAME)
		{
			return UpdateDNAME(dEPTNO, dNAME, null);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "tm">事务管理对象</param>
		/// <param name = "dEPTNO"></param>
		/// <param name = "dNAME"></param>
		/// <return></return>
		public virtual int UpdateDNAME(decimal dEPTNO, string dNAME, TransactionManager tm = null)
		{
			const string sql = @"UPDATE ""SCOTT"".""DEPT"" SET ""DNAME""=:DNAME  WHERE ""DEPTNO""=:DEPTNO";
			return AdoBase(sql)
							.AddInParameter(":DNAME", dNAME != null ?  (object)dNAME : DBNull.Value, DbType.AnsiString, 14)
							.AddInParameter(":DEPTNO", dEPTNO, DbType.VarNumeric, 0)
							.ExecuteNonQuery(tm);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "dEPTNO"></param>
		/// <param name = "lOC"></param>
		/// <return></return>
		public virtual int UpdateLOC(decimal dEPTNO, string lOC)
		{
			return UpdateLOC(dEPTNO, lOC, null);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "tm">事务管理对象</param>
		/// <param name = "dEPTNO"></param>
		/// <param name = "lOC"></param>
		/// <return></return>
		public virtual int UpdateLOC(decimal dEPTNO, string lOC, TransactionManager tm = null)
		{
			const string sql = @"UPDATE ""SCOTT"".""DEPT"" SET ""LOC""=:LOC  WHERE ""DEPTNO""=:DEPTNO";
			return AdoBase(sql)
							.AddInParameter(":LOC", lOC != null ?  (object)lOC : DBNull.Value, DbType.AnsiString, 13)
							.AddInParameter(":DEPTNO", dEPTNO, DbType.VarNumeric, 0)
							.ExecuteNonQuery(tm);
		}

		/// <summary>
		/// 按自定义条件更新数据
		/// </summary>
		/// <param name = "set">更新列</param>
		/// <param name = "where">更新条件</param>
		/// <param name = "paras">参数集合</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public int Update(string set, string where, IEnumerable<DbParameter> paras, TransactionManager tm = null)
		{
			if (string.IsNullOrEmpty(set)) throw new ArgumentNullException("set");
			var sql = string.Format(@"UPDATE ""SCOTT"".""DEPT"" SET {0} {1}", set, (string.IsNullOrEmpty(where) ? string.Empty : " WHERE " + where));
			return AdoBase(sql)
							.AddParameters(paras)
							.ExecuteNonQuery(tm);
		}
		#endregion

		#region Delete方法集合
		/// <summary>
		/// 按主键删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "dEPTNO"></param>
		public int DeleteByPK(decimal dEPTNO, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM ""SCOTT"".""DEPT"" WHERE ""DEPTNO""=:DEPTNO";
			return AdoBase(sql)
							.AddInParameter(":DEPTNO", dEPTNO, DbType.VarNumeric, 0)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "dNAME"></param>
		public int DeleteByDNAME(string dNAME, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM ""SCOTT"".""DEPT"" WHERE ""DNAME""=:DNAME";
			return AdoBase(sql)
							.AddInParameter(":DNAME", dNAME != null ?  (object)dNAME : DBNull.Value, DbType.AnsiString, 14)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "lOC"></param>
		public int DeleteByLOC(string lOC, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM ""SCOTT"".""DEPT"" WHERE ""LOC""=:LOC";
			return AdoBase(sql)
							.AddInParameter(":LOC", lOC != null ?  (object)lOC : DBNull.Value, DbType.AnsiString, 13)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 删除指定条件的记录
		/// </summary>
		/// <param name = "whereClause">自定义删除条件，where子句。如：id=1 and name='aaa'</param>
		/// <param name = "paras">where子句中定义的参数集合</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public int Delete(string whereClause, IEnumerable<DbParameter> paras, TransactionManager tm = null)
		{
			var sql = string.Format(@"DELETE FROM ""SCOTT"".""DEPT"" {0}", (string.IsNullOrEmpty(whereClause) ? string.Empty : " WHERE " + whereClause));
			return AdoBase(sql)
							.AddParameters(paras)
							.ExecuteNonQuery(tm);
		}
		#endregion

		#region  自定义方法集合
		#endregion

	}


	#endregion
}
